//
//  GPWorkController.m
//  GoldPig
//
//  Created by 虎爷 on 2020/1/6.
//  Copyright © 2020 虎爷. All rights reserved.
//

#import "GPWorkController.h"
#import "GPTipView.h"
@interface GPWorkController ()
@property (weak, nonatomic) IBOutlet UILabel *amount1;
@property (weak, nonatomic) IBOutlet UILabel *amount2;
@property (weak, nonatomic) IBOutlet UILabel *amount3;
@property (weak, nonatomic) IBOutlet UILabel *amount4;
@property (weak, nonatomic) IBOutlet UILabel *amount5;
@property (weak, nonatomic) IBOutlet UILabel *workSecond1;
@property (weak, nonatomic) IBOutlet UILabel *workSecond2;
@property (weak, nonatomic) IBOutlet UILabel *workSecond3;
@property (weak, nonatomic) IBOutlet UILabel *workSecond4;
@property (weak, nonatomic) IBOutlet UILabel *workSecond5;
@property (weak, nonatomic) IBOutlet UIButton *status1Btn;
@property (weak, nonatomic) IBOutlet UIButton *status2Btn;
@property (weak, nonatomic) IBOutlet UIButton *status3Btn;
@property (weak, nonatomic) IBOutlet UIButton *status4Btn;
@property (weak, nonatomic) IBOutlet UIButton *status5Btn;
@property (nonatomic,strong) NSArray *workList;
@property (nonatomic,strong) NSNumber *remainTime;
@property (weak, nonatomic) IBOutlet UILabel *remainTimeLab;
@property (weak, nonatomic) IBOutlet UILabel *workNameLab;
@property (weak, nonatomic) IBOutlet UIView *counDownBGView;
@property (weak, nonatomic) IBOutlet UIImageView *workBGImage;
@property (nonatomic, weak) NSTimer *timer;
@property (nonatomic,strong) NSNumber *bonusesTypeId;
@end

@implementation GPWorkController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self workSearchResquest];
}

- (void)setWorkList:(NSArray *)workList{
    _workList = workList;
    NSDictionary *d1 = workList[0];
    NSDictionary *d2 = workList[1];
    NSDictionary *d3 = workList[2];
    NSDictionary *d4 = workList[3];
    NSDictionary *d5 = workList[4];
    
    self.amount1.text = [NSString stringWithFormat:@"%@",d1[@"amount"]];
    self.amount2.text = [NSString stringWithFormat:@"%@",d2[@"amount"]];
    self.amount3.text = [NSString stringWithFormat:@"%@",d3[@"amount"]];
    self.amount4.text = [NSString stringWithFormat:@"%@",d4[@"amount"]];
    self.amount5.text = [NSString stringWithFormat:@"%@",d5[@"amount"]];
    
    NSNumber *workSecond1 = d1[@"workSecond"];
    NSNumber *workSecond2 = d2[@"workSecond"];
    NSNumber *workSecond3 = d3[@"workSecond"];
    NSNumber *workSecond4 = d4[@"workSecond"];
    NSNumber *workSecond5 = d5[@"workSecond"];
    self.workSecond1.text = [NSString stringWithFormat:@"%d分钟",workSecond1.intValue/60];
    self.workSecond2.text = [NSString stringWithFormat:@"%d分钟",workSecond2.intValue/60];
    self.workSecond3.text = [NSString stringWithFormat:@"%d分钟",workSecond3.intValue/60];
    self.workSecond4.text = [NSString stringWithFormat:@"%d分钟",workSecond4.intValue/60];
    self.workSecond5.text = [NSString stringWithFormat:@"%d分钟",workSecond5.intValue/60];
    
    NSNumber *status1 = d1[@"status"];
    NSNumber *status2 = d2[@"status"];
    NSNumber *status3 = d3[@"status"];
    NSNumber *status4 = d4[@"status"];
    NSNumber *status5 = d5[@"status"];
    [self.status1Btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"打工%@",status1]] forState:UIControlStateNormal];
    [self.status2Btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"打工%@",status2]] forState:UIControlStateNormal];
    [self.status3Btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"打工%@",status3]] forState:UIControlStateNormal];
    [self.status4Btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"打工%@",status4]] forState:UIControlStateNormal];
    [self.status5Btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"打工%@",status5]] forState:UIControlStateNormal];
    
    [self.status1Btn setTitle:[self setBtnTitle:status1.intValue] forState:UIControlStateNormal];
    [self.status2Btn setTitle:[self setBtnTitle:status2.intValue] forState:UIControlStateNormal];
    [self.status3Btn setTitle:[self setBtnTitle:status3.intValue] forState:UIControlStateNormal];
    [self.status4Btn setTitle:[self setBtnTitle:status4.intValue] forState:UIControlStateNormal];
    [self.status5Btn setTitle:[self setBtnTitle:status5.intValue] forState:UIControlStateNormal];
    
}

-(NSString *)setBtnTitle:(int)status{
    if (status == 0) {
        return @"去打工";
    }else if (status == 1){
        return @"打工中";
    }else if (status == 2){
        return @"领取薪水";
    }else{
        return @"无工作";
    }
}
- (IBAction)s1Click:(id)sender {
    NSDictionary *d1 = self.workList[0];
    NSNumber *status1 = d1[@"status"];
    if (status1.intValue == 2) {
        [self bonusesGetResquest:2800];
    }else if(status1.intValue == 0){
        [self doWorkResquest:2800];
    }
    
}
- (IBAction)s2Click:(id)sender {
    NSDictionary *d1 = self.workList[1];
    NSNumber *status1 = d1[@"status"];
    if (status1.intValue == 2) {
        [self bonusesGetResquest:2801];
    }else if(status1.intValue == 0){
        [self doWorkResquest:2801];
    }
}
- (IBAction)s3Click:(id)sender {
    NSDictionary *d1 = self.workList[2];
    NSNumber *status1 = d1[@"status"];
    if (status1.intValue == 2) {
        [self bonusesGetResquest:2802];
    }else if(status1.intValue == 0){
        [self doWorkResquest:2802];
    }
}

- (IBAction)s4Click:(id)sender {
    NSDictionary *d1 = self.workList[3];
    NSNumber *status1 = d1[@"status"];
    if (status1.intValue == 2) {
        [self bonusesGetResquest:2803];
    }else if(status1.intValue == 0){
        [self doWorkResquest:2803];
    }
    
}
- (IBAction)s5Click:(id)sender {
    NSDictionary *d1 = self.workList[4];
    NSNumber *status1 = d1[@"status"];
    if (status1.intValue == 2) {
        [self bonusesGetResquest:2804];
    }else if(status1.intValue == 0){
        [self doWorkResquest:2804];
    }
}
- (IBAction)backClick:(id)sender {
    [self.navigationController popViewControllerAnimated:NO];
}

-(void)workSearchResquest{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:@[[NSNumber numberWithInt:2800],[NSNumber numberWithInt:2801],[NSNumber numberWithInt:2802],[NSNumber numberWithInt:2803],[NSNumber numberWithInt:2804]] forKey:@"bonusesTypeIds"];
    [[GPRequsetManager HttpRequstManager] PostURL2:Tree_IP(workSearch) params:dic success:^(NSInteger code, id  _Nonnull result) {
        //NSLog(@"%@",result);
        
        if (code == 1) {
            
            NSDictionary *data = result[@"data"][0];
            self.workList = data[@"workList"];
            
            self.remainTime = data[@"remainTime"];
            self.bonusesTypeId = data[@"bonusesTypeId"];
            
            [self.timer invalidate];
            self.timer = nil;
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerHeadle) userInfo:nil repeats:YES];
            

        }else{
            NSString *msg = result[@"msg"];
            [SVProgressHUD showImage:NULL_IMAGE status:msg];
        }
        
        
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(void)doWorkResquest:(int)bid{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:bid] forKey:@"bonusesTypeId"];
    [[GPRequsetManager HttpRequstManager] PostURL2:Tree_IP(doWork) params:dic success:^(NSInteger code, id  _Nonnull result) {
        NSLog(@"%@",result);
        
        if (code == 1) {
            [self workSearchResquest];
        }else{
            NSString *msg = result[@"msg"];
            [SVProgressHUD showImage:NULL_IMAGE status:msg];
        }
        
        
    } fail:^(NSError * _Nonnull error) {
        
    }];
}
-(void)bonusesGetResquest:(int)bid{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    dic[@"bonusesTypeIds"] = @[[NSNumber numberWithInt:2800],[NSNumber numberWithInt:2801],[NSNumber numberWithInt:2802],[NSNumber numberWithInt:2803],[NSNumber numberWithInt:2804]];
    dic[@"bonusesTypeId"] = [NSNumber numberWithInt:bid];

    [[GPRequsetManager HttpRequstManager] PostURL2:Tree_IP(bonusesGet) params:dic success:^(NSInteger code, id  _Nonnull result) {
        //NSLog(@"%@",result);
        
        if (code == 1) {
            
            NSDictionary *data = result[@"data"][0];
            
            GPTipView *tipView = [GPTipView initGPTipView];
            tipView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
            [self.view.window addSubview:tipView];
            tipView.amount = data[@"amount"];
            
            if (bid == 2800 || bid ==2801) {
                tipView.name  = @"金币";
            }else{
                tipView.name  = @"钻石";
            }

            [self workSearchResquest];
        }else{
            NSString *msg = result[@"msg"];
            [SVProgressHUD showImage:NULL_IMAGE status:msg];
        }
        
        
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(void)timerHeadle{
    if (self.remainTime.integerValue<=0) {
        [self.timer invalidate];
        self.timer = nil;
        
        [self.counDownBGView setHidden:YES];

        //        self.homeView.diamondsTimeLab.text = @"可领取";
    }else{
        [self.counDownBGView setHidden:NO];
        self.remainTime = [NSNumber numberWithInt:self.remainTime.intValue - 1];
    }
}

- (void)setRemainTime:(NSNumber *)remainTime{
    _remainTime = remainTime;
    
    self.remainTimeLab.text = [self hoursFormatted:remainTime.integerValue];
}

- (void)setBonusesTypeId:(NSNumber *)bonusesTypeId{
    _bonusesTypeId = bonusesTypeId;
    
    if (bonusesTypeId.intValue == 2800) {
        self.workNameLab.text = @"洗盘子";
        self.workBGImage.image = [UIImage imageNamed:@"洗盘子背景"];
    }else if (bonusesTypeId.intValue == 2801){
        self.workNameLab.text = @"洗盘子";
        self.workBGImage.image = [UIImage imageNamed:@"打扫家背景"];
    }else if (bonusesTypeId.intValue == 2802){
        self.workNameLab.text = @"洗盘子";
        self.workBGImage.image = [UIImage imageNamed:@"去搬砖背景"];
    }else if (bonusesTypeId.intValue == 2803){
        self.workNameLab.text = @"开专车";
        self.workBGImage.image = [UIImage imageNamed:@"开专车背景"];
    }else if (bonusesTypeId.intValue == 2804){
        self.workNameLab.text = @"写代码";
        self.workBGImage.image = [UIImage imageNamed:@"写代码背景"];
    }
    
    
}
- (NSString *)hoursFormatted:(NSInteger)totalSeconds {
    NSInteger seconds = totalSeconds % 60;
    NSInteger minutes = (totalSeconds / 60) % 60;
    NSInteger hours = totalSeconds / 3600;
    if (hours) {
        return [NSString stringWithFormat:@"%02ld:%02ld:%02ld",hours, minutes, seconds];
    }
    return [NSString stringWithFormat:@"%02ld:%02ld",  minutes, seconds];
}
@end
